package com.muyu.common.security.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.muyu.common.core.basic.SecurityBasic;
import lombok.AllArgsConstructor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.util.Date;

/**
 * myBatsPlus字段自动填充
 * @author DongZeLiang
 * @date 2025/03/02 22:24
 */
@AllArgsConstructor //全参注入
@ConditionalOnProperty(value = "mybatis-plus.meta.enabled", havingValue = "true")
public class SystemMetaObjectHandler implements MetaObjectHandler {

    private final SecurityBasic securityBasic;

    /**
     * 数据库字段插入
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createBy", String.class, securityBasic.getUsername());
        this.strictInsertFill(metaObject,"createTime", Date.class,new Date());
    }

    /**
     * 数据库字段修改
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateBy", String.class, securityBasic.getUsername());
        this.strictUpdateFill(metaObject,"updateTime", Date.class,new Date());
    }

}
